// 发送网络请求的模块
const http = require('http')
const { EventEmitter } = require('events')

class myRequest extends EventEmitter {
    constructor(url, options) {
        super()
        this.url = url
        this.options = options
    }
    send(body = '') {
        const request = http.request(this.url, this.options, res => {
            let result = "";
            res.on('data', chunk => {
                result += chunk.toString('utf-8');
            })
            res.on('end', () => {
                this.emit('res', result)
            })
        })
        request.write(body)
        request.end()
    }
}
// module.exports = myRequest

// 使用
const request = new myRequest('http://www.baidu.com', { methods: "POST" })
// 发送请求
request.send()
// 接收响应
request.on('res', (res) => {
    console.log(res)
})